﻿using System;
using Core.AdminGroupMenuPermission;
using Core.Authenticated;
using Core.Library;
using DataObject;
using DataObject.Cache;
using DataObject.Members;

namespace CMS.Controls.Members
{
    public partial class Edit : AuthenticatedControl
    {
        protected string ReturnUrl;
        protected string CurrentTab;
        protected string Action;

        protected void Page_Load(object sender, EventArgs e)
        {
            ReturnUrl = Utils.GetIdFromRequestString(Utils.ReturnUrl, string.Empty);
            Action = Utils.GetIdFromRequestString(Utils.Action, string.Empty);

            if (!IsPostBack)
            {
                var id = Utils.GetIdFromRequestString(Utils.Id, 0);
                ltTitle.Text = id > 0 ? "Sửa bài" : "Tạo mới bài";

                ActionPermission();
                if (id > 0)
                    BindObject(id);
            }
        }

        public void ActionPermission()
        {
            var isShowPublished = false;
            var isApprove = false;

            var adminGroupMenuPermission =
                    AdminGroupMenuPermissionController.AdminGroupMenuPermission_GetBy_AdminUserId_AdminMenuId(CurrentUser.AdminUserId, cControl.AdminMenuId);
            if (adminGroupMenuPermission.Count > 0)
            {
                foreach (var agmp in adminGroupMenuPermission)
                {
                    isShowPublished = StatusHelper.IsChecked(agmp.Status, (int)PermissionObject.CanPublish);
                    isApprove = StatusHelper.IsChecked(agmp.Status, (int)PermissionObject.CanReturn);
                }
            }

            foreach (var t in Utils.Tabs)
                if (ReturnUrl.IndexOf(t) != -1)
                    CurrentTab = t;

            if (!string.IsNullOrEmpty(CurrentTab))
            {
                switch (CurrentTab)
                {
                    // Đã gỡ                        
                    case "IsRemoved":
                        btnRestore.Visible = isShowPublished || isApprove;
                        btnSaveWithoutStatus.Visible = true;
                        break;
                    // Chờ duyệt                        
                    case "IsWaitingForPublish":
                        btnPublish.Visible = isShowPublished || isApprove;
                        btnRemove.Visible = isShowPublished || isApprove;
                        btnSaveWithoutStatus.Visible = true;
                        break;
                    // Duyệt                     
                    case "IsPublished":
                        btnRound1.Visible = isShowPublished || isApprove;
                        btnRemove.Visible = isShowPublished || isApprove;
                        btnSaveWithoutStatus.Visible = isShowPublished || isApprove;
                        break;
                    // Vòng 1                     
                    case "Round1":
                        btnRound2.Visible = isShowPublished || isApprove;
                        btnRemove.Visible = isShowPublished || isApprove;
                        btnSaveWithoutStatus.Visible = isShowPublished || isApprove;
                        break;
                    // Vòng 2                     
                    case "Round2":
                        btnRound3.Visible = isShowPublished || isApprove;
                        btnRemove.Visible = isShowPublished || isApprove;
                        btnSaveWithoutStatus.Visible = isShowPublished || isApprove;
                        break;
                    // Vòng 3                   
                    case "Round3":
                        btnRemove.Visible = isShowPublished || isApprove;
                        btnSaveWithoutStatus.Visible = isShowPublished || isApprove;
                        break;
                }

                txtVote1.Enabled = isShowPublished;
                txtVote2.Enabled = isShowPublished;
                txtVote3.Enabled = isShowPublished;
                txtCheat.Enabled = isShowPublished;
            }
        }

        private void BindObject(int id)
        {
            var o = MembersController.Members_GetById(id);
            if (o == null) return;

            var lstCity = MembersController.City_GetAll();
            ddlCity.DataSource = lstCity;
            ddlCity.DataTextField = Constants.Table_City.City_Name;
            ddlCity.DataValueField = Constants.Table_City.City_ID;
            ddlCity.DataBind();
            txtName.Text = o.Member_Name;
            txtFullName.Text = o.Member_FullName;
            txtVote1.Text = o.Vote1.ToString();
            txtVote2.Text = o.Vote2.ToString();
            txtVote3.Text = o.Vote3.ToString();
            ddlGender.SelectedValue = o.Member_Gender.ToString();
            txtBirthday.Text = DateTimeFunctions.FormatDateVietNam(o.Member_Birthday);
            txtEmail.Text = o.Member_Email;
            txtAddress.Text = o.Member_Address;
            ddlCity.SelectedValue = o.Member_City.ToString();
            txtTel.Text = o.Member_Tel;
            txtAbout.Text = o.Member_About;
            txtAvatar.Text = o.Member_Avatar;
            txtOriginalAvatar.Text = o.Member_OriginalAvatar;
            cbIsHomePage.Checked = o.Member_HomePageFocus;
            cbIsWin.Checked = o.Member_IsWin;
            cbIsSlide.Checked = o.Member_IsSlide;
            txtCheat.Text = o.isCheatVote.ToString();
        }

        public void ManagerAction(string action)
        {

            MembersObject o;

            int id = Utils.GetIdFromRequestString(Utils.Id, 0);

            if (id > 0)
            {
                o = MembersController.Members_GetById(id);
                var birthday = DateTimeFunctions.ValidDateTime(txtBirthday.Text);
                var gender = ConvertUtility.ConvertTo<int>(ddlGender.SelectedValue);
                var city = ConvertUtility.ConvertTo<int>(ddlCity.SelectedValue);
                var vote1 = ConvertUtility.ConvertTo<long>(txtVote1.Text);
                var vote2 = ConvertUtility.ConvertTo<long>(txtVote2.Text);
                var vote3 = ConvertUtility.ConvertTo<long>(txtVote3.Text);
                var cheat = ConvertUtility.ConvertTo<byte>(txtCheat.Text);
                var currentStatus = o.Member_Status;
                var currentRound = o.Member_InRound;
                switch (action)
                {
                    case Const.Action.Publish:
                        currentStatus = (int)MemberStatus.IsPublished;
                        currentRound = (int)MemberRound.NoRound;
                        break;
                    case Const.Action.Remove:
                        currentStatus = (int)MemberStatus.IsRemoved;
                        currentRound = (int)MemberRound.NoRound;
                        break;
                    case Const.Action.Restore:
                        currentStatus = (int)MemberStatus.IsWaitingForPublish;
                        currentRound = (int)MemberRound.NoRound;
                        break;
                    case Const.Action.Round1:
                        currentStatus = (int)MemberStatus.IsPublished;
                        currentRound = (int)MemberRound.Round1;
                        break;
                    case Const.Action.Round2:
                        currentStatus = (int)MemberStatus.IsPublished;
                        currentRound = (int)MemberRound.Round2;
                        break;
                    case Const.Action.Round3:
                        currentStatus = (int)MemberStatus.IsPublished;
                        currentRound = (int)MemberRound.Round3;
                        break;
                }
                o.Member_ID = id;
                o.Member_Name = txtName.Text;
                o.Member_FullName = txtFullName.Text;
                o.Vote1 = vote1;
                o.Vote2 = vote2;
                o.Vote3 = vote3;
                o.Member_Gender = gender;
                o.Member_Birthday = birthday;
                o.Member_Email = txtEmail.Text;
                o.Member_Address = txtAddress.Text;
                o.Member_City = city;
                o.Member_Tel = txtTel.Text;
                o.Member_About = txtAbout.Text;
                o.Member_JobAddress = txtAddress.Text;
                o.Member_Avatar = txtAvatar.Text;
                o.Member_OriginalAvatar = txtOriginalAvatar.Text;
                o.Member_Status = currentStatus;
                o.Member_InRound = currentRound;
                o.Member_HomePageFocus = cbIsHomePage.Checked;
                o.Member_IsWin = cbIsWin.Checked;
                o.Member_IsSlide = cbIsSlide.Checked;
                o.isCheatVote = cheat;

                MembersController.Members_Update(o);
            }
            CacheController.RemoveCachePort(Constants.ManagerCacheGroup.User);
            if (action != Const.Action.SaveAndContinue) Response.Redirect(ReturnUrl);
        }

        #region Buttons


        protected void BtnSaveWithoutStatus(object sender, EventArgs e)
        {
            ManagerAction(Const.Action.SaveWithoutStatus);
        }

        protected void BtnSaveAndContinue(object sender, EventArgs e)
        {
            ManagerAction(Const.Action.SaveAndContinue);
        }

        protected void BtnRound3(object sender, EventArgs e)
        {
            ManagerAction(Const.Action.Round3);
        }

        protected void BtnRound2(object sender, EventArgs e)
        {
            ManagerAction(Const.Action.Round2);
        }

        protected void BtnRound1(object sender, EventArgs e)
        {
            ManagerAction(Const.Action.Round1);
        }

        protected void BtnPublish(object sender, EventArgs e)
        {
            ManagerAction(Const.Action.Publish);
        }

        protected void BtnRemove(object sender, EventArgs e)
        {
            ManagerAction(Const.Action.Remove);
        }

        protected void BtnRestore(object sender, EventArgs e)
        {
            ManagerAction(Const.Action.Restore);
        }

        #endregion
    }
}